<template>
  <div class="salesmanAdd">
    <gl-breadcrumb class="mb15"
                   :text="nav"></gl-breadcrumb>
    <div class="main">
      <div class="orderform">
        <gl-form :form='form'
                 :form-label='formLabel'
                 :must="must"
                 :rule='rule'
                 ref='form'
                 @change="formSelectChange">
        
        </gl-form>
        <div class="btn">
          <el-button @click="$router.go(-1)">取 消</el-button>
          <el-button type="primary"
                     @click="submitadmin">确 定</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'SalesmanAdd',
  components: {},
  data() {
    return {
      nav: [],
      form: {
        status: 1
      },
      formLabel: [
        {
          label: '业务员:',
          prop: 'bus_name',
          inputType: 'text',
          placeholder: '请输入'
        },
        {
          label: '联系方式:',
          prop: 'phone',
          inputType: 'text',
          placeholder: '请输入手机号'
        },
        {
          label: '所属公司:',
          prop: 'company_id',
          options: []
        },
        {
          label: '状态:',
          prop: 'status',
          radio: [{ label: '正常', value: 1 }, { label: '禁用', value: 2 }]
        },
        {
          label: '备注:',
          prop: 'remark',
          type: 'textarea',
          text: 'ueditor',
          placeholder: '请输入备注'
        }
      ],
      must: ['status'],
      rule: {
        bus_name: [
          {
            required: true,
            message: '请输入业务员名称',
            trigger: 'blur'
          }
        ],
        phone: [
          {
            required: true,
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确手机号码'
          }
        ],
        company_id: [
          { required: true, message: '请选择所属公司', trigger: 'change' }
        ]
      },
      companyList: [],
      num: ''

    }
  },
  computed: {
    ...mapGetters(['listInfo', 'userInfo'])
  },
  watch: {},
  methods: {
   
    // 提交
    submitadmin() {
      if (this.$refs.form.validate()) {
        if (this.num == 1) {
          this.subEdit()
        } else {
          this.subAdd()
        }
      }
    },
    // 新建提交
    subAdd() {
      let params = {
        bus_name: this.form.bus_name,
        admin_id: this.userInfo.id,
        company_id: this.form.company_id,
        phone: this.form.phone,
        status: this.form.status,
        remark: this.form.remark
      }
      this.http
        .$post('admin/salesman.Api/add', params)
        .then(res => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.$router.push('/setting/salesman')
        })
        .catch(err => {
          this.$message.error(err.msg)
        })
    },
    // 编辑提交
    subEdit() {
      let params = {
        bus_id: this.listInfo.bus_id,
        bus_name: this.form.bus_name,
        admin_id: this.userInfo.id,
        company_id: this.form.company_id,
        phone: this.form.phone,
        status: this.form.status,
        remark: this.form.remark
      }
      this.http
        .$post('admin/salesman.Api/edit', params)
        .then(res => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.$router.push('/setting/salesman')
        })
        .catch(err => {
          this.$message.error(err.msg)
        })
    },
    // 请求公司
    getCompany() {
      this.http
        .$post('admin/customer.api/list_company', {
          company_id: this.userInfo.company_id
        })
        .then(res => {
          for (let i = 0; i < res.length; i++) {
            this.formLabel[2].options.push({
              label: res[i].company_name,
              value: res[i].company_id
            })
          }
        })
        .catch(err => {
          this.$message.error(err.msg)
        })
    }
  },
  created() {
    this.nav = ['业务员管理', '新建业务员']
    this.num = this.$route.query.num
    this.getCompany()
    if (this.num == 1) {
      this.nav = ['业务员管理', '编辑业务员']
      let new_form = this.listInfo
      new_form.status = new_form.status == '正常' ? 1 : 2
      this.form = new_form
    }
  },
  mounted() {}
}
</script>
<style lang='less' scoped>
/deep/ .edui-default .edui-toolbar {
  line-height: 0;
}

.main {
  background-color: #fff;
  margin-top: 20px;
  p {
    height: 40px;
    .radio_type {
      display: inline-block;
      width: 14px;
      height: 14px;
      border: 1px solid rgba(153, 153, 153, 1);
      border-radius: 50%;
      margin-right: 10px;
      vertical-align: middle;
      cursor: pointer;
    }
    .state {
      font-size: 14px;
      font-weight: 400;
      color: rgba(34, 34, 34, 1);
      padding-right: 22px;
      vertical-align: middle;
      cursor: pointer;
    }
    .active {
      width: 14px;
      height: 14px;
      border: 1px solid #1890ff;
      position: relative;
    }
    .active::before {
      content: '';
      display: block;
      width: 10px;
      height: 10px;
      background: #1890ff;
      border-radius: 50%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
}
.orderform {
  width: 550px;
  margin: 0 auto;
  padding: 30px 0;
  .btn {
    text-align: center;
  }
}
</style>